import { Component, OnInit, Output,Input, EventEmitter, ViewChild } from '@angular/core';
import { Service } from '../../../server/server';
import {CudTableService} from '../../../public/serve/cud-table/cud-table.service';
import {HintsService} from '../../../public/serve/hints/hints.service';
import { RemoveTableService } from 'app/public/serve/remove-table/remove-table.service';
import { InitService } from 'app/public/serve/init/init.service';
import {MessageService} from 'primeng/api';
import { DatePipe } from '@angular/common';

declare var $: any;
@Component({
  selector: 'app-database-config',
  templateUrl: './database-config.component.html'
})
export class DatabaseConfigComponent implements OnInit {

  databaseType : any = 'backup';
  @Output() table = new EventEmitter<void>();

  constructor(
    private server: Service,
    public initService: InitService,
    private request: CudTableService,
    private hintsService: HintsService,
    public removeTableService: RemoveTableService,
    private datePipe: DatePipe,
  ) { }
  exportPathList: any;
  loading :boolean = false;
  ngOnInit() {
    this.getExportPathList();
  }

  //改变操作数据库工具的类型
  _databaseType(type:any){
    this.databaseType = type;
  }

    /**
     * 导出路径列表
     */
  getExportPathList() {
        let data = {
            token:'b25dbd7138cf48e09941087e6d9a2e93'
        };
        let url = this.server.baseUrl + 'Service/ExportPath/getExportPathList';
        this.request.postRequest(url, data).then((res: any) => {
            if (res.result_code === '0000') {
                this.exportPathList = res.result;
            } else {
                this.hintsService.messages(res.message, res.result_code);
            }
            return;
        })
            .catch((err: any) => {
                this.hintsService.messages("error", "error");
                return;
            });
    }

    //备份
  _backup(data:any){
    
    this.loading = true;
    let url = this.server.baseUrl + 'Service/Config/backUpDatabase';
    data.token = 'b25dbd7138cf48e09941087e6d9a2e93';
    data.fileName = 'DBBackupPath_db_'+this.datePipe.transform(new Date(), 'yyyyMMdd');
    data.createBy = this.server.getuserName();
    data.type = 0;
    this.request
      .postRequest(url, data)
      .then(res => {
        if (res.result_code === "0000") {
            this.table.emit();
        }
        this.hintsService.messages(res.message, res.result_code);
        this._initAdd();
        this.loading = false;
        return;
      })
      .catch(err => {
        this.loading = false;
        this.hintsService.messages("error", "error");
        this._initAdd();
        return;
      });
  }

  //还原
  _restore(){
    this.loading = true;
    let url = this.server.baseUrl + 'Service/Config/restoreDatabase';
    let data = { 
      token: 'b25dbd7138cf48e09941087e6d9a2e93',
      Id:this.removeTableService.getRemoveTableId
    };
    this.request
      .postRequest(url, data)
      .then(res => {
        if (res.result_code === "0000") {
            this.table.emit();
        }
        this.hintsService.messages(res.message, res.result_code);
        this._initAdd();
        this.loading = false;
        return;
      })
      .catch(err => {
        this.hintsService.messages("error", "error");
        this._initAdd();
        this.loading = false;
        return;
      });
  }

  /**
    * 初始化
    */
   private _initAdd(){
    this.initService._init();
    $("#databaseBackupOrRestore").modal("hide");
   }
}


